package com.learn.educourseboot.controller;

import com.alibaba.fastjson.JSON;
import com.learn.eduapi.entity.Course;
import com.learn.educourseboot.config.RabbitMQConfig;
import com.learn.educourseboot.service.CourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @BelongsProject: edu-learn
 * @Author:
 * @CreateTime: 2020-10-16 16:17
 * @Description:
 */
@Slf4j
@Api
@RestController
@RequestMapping("course")
public class CourseController {

    @Autowired
    private CourseService courseService;


    @GetMapping("getAllCourse")
    public List<Course> getAllCourse() {
        return courseService.getAllCourse();
    }

    @GetMapping("getCourseById/{courseid}")
    public Course getCourseById(@PathVariable("courseid")Integer courseid) {
        Course course = courseService.getCourseById(courseid);
        return course;
    }

    @GetMapping("getMyCoursesById")
    public List<Course> getMyCoursesById( String ids ) {
        System.out.println("待查询的课程id：" +ids);

        List<String> idList = new ArrayList<>();
        String id = null;
        while(ids.indexOf("=")>0) {
            if (ids.indexOf("&") > 0) {
                id = ids.substring(ids.indexOf("=") + 1, ids.indexOf("&"));
                System.out.println(id);
                ids = ids.substring(ids.indexOf("&")+1);
            } else {
                id = ids.substring(ids.indexOf("=") + 1);
                System.out.println(id);
                ids = ids.substring(ids.indexOf("=")+1);
            }
            idList.add(id);
        }
        System.out.println(idList);  //[7, 9]
        List<Course> list = courseService.getMyCoursesById(idList);
        return list;
    }

    @Autowired
    RabbitTemplate rabbitTemplate;

    @ApiOperation("添加课程")
    @PostMapping
    public ResponseEntity<Course> saveCourse(@RequestBody Course course){
        //添加到mysql
        courseService.save(course);
        //发送给mq,通知搜索服务进行添加
        rabbitTemplate.convertAndSend(RabbitMQConfig.COURSE_EXCHANGE,
                RabbitMQConfig.KEY_COURSE_SAVE, JSON.toJSONString(course),
                new CorrelationData(UUID.randomUUID().toString()));
        log.info("添加课程完成");
        return ResponseEntity.ok(course);
    }

    @ApiOperation("更新课程")
    @PutMapping
    public ResponseEntity<Course> updateCourse(@RequestBody Course course){
        courseService.updateById(course);
        rabbitTemplate.convertAndSend(RabbitMQConfig.COURSE_EXCHANGE,
                RabbitMQConfig.KEY_COURSE_SAVE, JSON.toJSONString(course),
                new CorrelationData(UUID.randomUUID().toString()));
        log.info("更新课程完成");
        return ResponseEntity.ok(course);
    }

    @ApiOperation("删除课程")
    @DeleteMapping("/{id}")
    public ResponseEntity<Long> deleteCourse(@PathVariable("id")Long id){
        courseService.removeById(id);
        rabbitTemplate.convertAndSend(RabbitMQConfig.COURSE_EXCHANGE,
                RabbitMQConfig.KEY_COURSE_REMOVE, id);
        log.info("删除课程");
        return ResponseEntity.ok(id);
    }
}
